package com.fivestar.pmanager.service;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fivestar.pmanager.core.base.BaseCommonParameterBean;
import com.fivestar.pmanager.core.exception.DMLNoRecordEffectedException;
import com.fivestar.pmanager.core.service.BaseServiceImpl;
import com.fivestar.pmanager.core.util.JsonUtil;
import com.fivestar.pmanager.dao.mapper.SysDictMapper;
import com.fivestar.pmanager.model.SysDictBean;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 /**
 * 字典表
 * SysDictService
 * @author 
 */
@Service("sysDictService")
public class SysDictService extends BaseServiceImpl<SysDictBean>
{
	private final static Logger log = LoggerFactory.getLogger(SysDictService.class);
	@Autowired
    private SysDictMapper mapper;


    @Override
    public int insert(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        sysDict.initBeforeCreate(commonParam.getActorId());
        int result;
        if ((result = mapper.insert(sysDict)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int delete(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        sysDict.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.delete(sysDict)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }

    @Override
    public int deletePhysics(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException {
        int result;
        if ((result = mapper.deletePhysics(sysDict)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public int update(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        sysDict.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.update(sysDict)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }
    
    
    @Override
    public int updateBySelective(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        sysDict.initBeforeUpdate(commonParam.getActorId());
        int result;
        if ((result = mapper.updateBySelective(sysDict)) != 1)
        {
            throw new DMLNoRecordEffectedException();
        }
        return result;
    }


    @Override
    public List<SysDictBean> getList(SysDictBean sysDict)
    {
        return mapper.getList(sysDict);
    }


    @Override
    public SysDictBean getSingleById(Integer arg0)
    {
        return mapper.getSingleById(arg0);
    }


    @Override
    public int getCountOfPage(SysDictBean sysDict)
    {
        return mapper.getCountOfPage(sysDict);
    }


    @Override
    public List<SysDictBean> getListOfPage(SysDictBean sysDict)
    {
        return mapper.getListOfPage(sysDict);
    }


	@Override
    public int getCount(SysDictBean sysDict)
    {
        return mapper.getCount(sysDict);
    }


    @Override
    public void save(SysDictBean sysDict, BaseCommonParameterBean commonParam)
            throws DMLNoRecordEffectedException
    {
        if(sysDict.getRowId() == null)
        {
            insert(sysDict, commonParam);
        }
        else
        {
            updateBySelective(sysDict, commonParam);
        }
    }


    @Override
    public SysDictBean getSingle(SysDictBean sysDict)
    {
        return mapper.getSingle(sysDict);
    }

    /**
     * 根据名字获取字典列表
     *
     * @param name
     * @return 字典
     */
    public List<SysDictBean> findByName(String name)
    {
        return mapper.findByName(name);
    }



    public String generateJs(String path) throws IOException
    {
        List<SysDictBean> list = getList(null);
        Map<String,Map<String,Object>> dictMaps = new HashMap<>();
        for (SysDictBean dictBean : list)
        {
            Map<String, Object> dictMap = dictMaps.get(dictBean.getName());
            if(dictMap == null) dictMap = new HashMap<>();
            dictMap.put(dictBean.getCode(), dictBean.getValue());
            dictMaps.put(dictBean.getName(), dictMap);
        }

        String dictJs = "var SM_DICT = " + JsonUtil.writeMap2Json(dictMaps) + ";SM_DICT.getValue=function(name,code){if(code != undefined && code != null && code != 'undefined' && 'code' != null){if(this[name] != undefined){if(this[name][code] != undefined){return this[name][code];}else{return '';}}return '';} return '';};SM_DICT.getValues=function(name){if(this[name] != undefined) {return this[name]}else {return {};}};";
        FileUtils.writeStringToFile(new File(path), dictJs, "utf-8");
        log.debug("生成字典js保存路径：{}", path);
        return dictJs;
    }


    public SysDictBean findByNameAndCode(String name, String code)
    {
        return mapper.findByNameAndCode(name,code);
    }
    
    /**
     * 查找记录返回listmap
     *
     * @param SysDictBean 查询条件
     * @return List<Map<String,Object>>
     */
    public List<Map<String,Object>> findForSearch(SysDictBean SysDictBean)
    {
        return mapper.findForSearch(SysDictBean);
    }



}
